<template>
  <el-form ref="newFloorForm" :model="newFloorForm" label-width="120px">
    <el-form-item label="楼栋：">{{ activeBuilding.name }}</el-form-item>
    <el-form-item
      label="名称："
      prop="name"
      :rules="[{ required: true, message: '请输入名称' }]"
    >
      <el-input
        placeholder="请输入"
        type="text"
        v-model.trim="newFloorForm.name"
      ></el-input>
    </el-form-item>
    <!-- 平面图 -->
    <el-form-item
      label="平面图："
      prop="path"
      :rules="[{ required: true, message: '请上传平面图' }]"
    >
      <el-upload
        class="upload-demo"
        :action="config.uploadUrl"
        :on-success="handleImageSuccess"
        :before-upload="beforeImageUpload"
        :show-file-list="false"
        accept="image/*"
        :data="{ token: siteInfos.token }"
      >
        <el-button size="small" type="primary">点击上传</el-button>
      </el-upload>
      <div class="mt-10" style="width:50%" v-if="newFloorForm.path">
        <floorMarker
          :img="newFloorForm.path"
          :coordList="coordinateData"
          height="auto"
        ></floorMarker>
      </div>
    </el-form-item>
    <!-- 设备列表： -->
    <el-form-item
      label="设备列表："
      :rules="[{ required: true, message: '请添加设备' }]"
    >
      <ul class="device-list mb-10" v-if="coordinateData.length > 0">
        <li v-for="(item, index) in coordinateData" :key="index">
          <p class="flex-1">
            {{ item.deviceName }}
          </p>
          <p class="color-gray mr-10">坐标：{{ item.x }},{{ item.y }}</p>
          <el-button
            size="mini"
            type="green"
            @click="markDevice(item)"
            :disabled="!$store.getters.hasEditPermission"
            >标注位置</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="deleteDevice(index)"
            :disabled="!$store.getters.hasEditPermission"
            >删除</el-button
          >
        </li>
      </ul>
      <el-button
        size="small"
        type="primary"
        @click="openDeviceModal"
        :disabled="!$store.getters.hasEditPermission"
        >添加设备</el-button
      >
    </el-form-item>
    <!-- 标注设备位置弹窗 start -->
    <div class="floor-marker-modal" v-if="markerVisible">
      <p>在图片上点击鼠标左键添加设备位置</p>
      <floorMarker
        :img="newFloorForm.path"
        :activeDevice="activeDevice"
        :markable="true"
        ref="floorMarker"
      ></floorMarker>
      <div class="text-center">
        <el-button size="small" type="default" @click="markerVisible = false"
          >取消</el-button
        >
        <el-button
          size="small"
          type="primary"
          @click="confirmMarker"
          :disabled="!$store.getters.hasEditPermission"
          >确定</el-button
        >
      </div>
    </div>
    <!-- 标注设备位置弹窗 end -->
    <!-- 选择设备 弹窗 start -->
    <el-dialog
      title="选择设备"
      v-if="deviceVisible"
      :visible.sync="deviceVisible"
      width="1000px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      destroy-on-close
      append-to-body
    >
      <FlooDeviceSelect
        :businessId="businessId"
        :coordinateData="coordinateData"
        @select="onDeviceSelect"
      ></FlooDeviceSelect>
    </el-dialog>
    <!-- 选择设备 弹窗 end -->
  </el-form>
</template>
<script>
import floorMarker from "@/components/archive/FloorMarker";
import FlooDeviceSelect from "@/components/archive/FloorDeviceSelect";
import { mapState } from "vuex";

export default {
  components: {
    floorMarker,
    FlooDeviceSelect,
  },
  data() {
    return {
      newFloorForm: {
        name: "",
        floorId: "", //楼栋ID
        path: "", //'https://file.wiscloud.online/20220801/1659314668427.png',
        coordinateData: [],
      },
      coordinateData: [],
      markerVisible: false,
      activeDevice: null,
      deviceList: [],
      deviceVisible: false,
    };
  },
  props: {
    activeData: {
      type: Object,
      default() {
        return null;
      },
    },
    activeBuilding: {
      type: Object,
      default() {
        return {};
      },
    },
    businessId: {
      type: String,
      default: "",
    },
  },
  computed: {
    ...mapState(["siteInfos"]),
    isAdmin() {
      return this.siteInfos && this.siteInfos.userType == 1;
    },
  },
  created() {
    if (this.activeData) {
      this.newFloorForm = this.activeData;
      this.coordinateData = this.activeData.coordinateData;
    }
  },
  methods: {
    handleImageSuccess(res, file) {
      this.newFloorForm.path = res.data.filePath;
      this.closeFullLoading();
    },
    beforeImageUpload(file) {
      // const isLt2M = file.size / 1024 / 1024 < 2;

      // if (!isLt2M) {
      //     this.$message.error('上传头像图片大小不能超过 2MB!');
      // }
      // return isLt2M;
      this.openFullLoading("正在上传");
      return true;
    },
    openDeviceModal() {
      this.deviceVisible = true;
    },
    onDeviceSelect(array) {
      // this.coordinateData.push({
      //   deviceId: item.id,
      //   deviceName: item.name,
      //   x: 0,
      //   y: 0,
      // });
      this.coordinateData = array.map((item) => {
        return {
          deviceId: item.id,
          deviceName: item.name,
          x: 0,
          y: 0,
        };
      });
      console.log(this.coordinateData);
      this.deviceVisible = false;
    },
    markDevice(item) {
      if (!this.newFloorForm.path) {
        this.$message.error("请上传平面图");
        return;
      }
      this.activeDevice = item;
      this.markerVisible = true;
    },
    confirmMarker() {
      const newMarker = this.$refs.floorMarker.getNewMarker();
      if (!newMarker) {
        this.$message.error("请标注位置");
        return;
      }
      this.activeDevice.x = newMarker.x;
      this.activeDevice.y = newMarker.y;
      this.markerVisible = false;
    },
    submitAction() {
      this.$refs["newFloorForm"].validate((valid) => {
        if (valid) {
          if (this.coordinateData.length == 0) {
            this.$message.error("请添加设备");
            return;
          }
          this.newFloorForm.floorId = this.activeBuilding.id;
          this.newFloorForm.coordinateData = JSON.parse(
            JSON.stringify(this.coordinateData)
          );
          this.adding = true;
          this.$post(
            "/business/firefightingplan/storeyAddOrUpdate",
            this.newFloorForm
          ).then(
            (res) => {
              this.$message.success("保存成功");
              this.$emit("success");
            },
            (error) => {
              this.$emit("fail");
              this.adding = false;
              this.$message.error(error.msg);
            }
          );
        }
      });
    },
    deleteDevice(index) {
      this.showConfirm("确定删除该设备吗？", () => {
        this.coordinateData.splice(index, 1);
      });
    },
  },
};
</script>
<style scoped lang="less">
.marker-img {
  width: 50%;
  & > img {
    display: block;
    width: 100%;
  }
}
.device-list {
  padding: 0 10px;
  border: 1px solid rgba(255, 255, 255, 0.5);
  & > li {
    display: flex;
    align-items: center;
    height: 50px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.2);
    &:last-child {
      border-bottom: none;
    }
  }
}
</style>
